//
//  AddMessageBedViewController.m
//  Spa
//
//  Created by PangPond on 14/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "AddMessageBedViewController.h"
#import "Bed.h"
@interface AddMessageBedViewController ()

@end

@implementation AddMessageBedViewController

@synthesize databaseUtility = _databaseUtility;
@synthesize keyBedName = _keyBedName;
@synthesize keyIf;
@synthesize tableView = _tableView;

//cell
@synthesize addBedNameCell = _addBedNameCell;
@synthesize fieldNameCell;

@synthesize addBedStatusCell = _addBedStatusCell;
@synthesize fieldStatusCell;


- (IBAction)dismissKeyboard:(id)sender
{
	[sender resignFirstResponder];
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //[self.tableView reloadData];
    [self loadView];
    [self viewDidLoad];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *bed = [self.databaseUtility fetchedResultsWithClass:@"Bed" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",self.keyBedName]];
    
    Bed *beds = [bed lastObject];
    
        static NSString *CellIdentifier = @"AddBedNameCell";
        
        AddBedNameCell *cell = (AddBedNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddBedNameCell" owner:self options:nil];
            
            cell = self.addBedNameCell;
            self.addBedNameCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = beds.name;
        return cell;

    
}

-(IBAction)BackButton:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//validation

-(BOOL)validate: (NSString *) candidate{
    NSString *bedName = @"[A-Z0-9a-zก-ฮ๑-๙.]{1,50}";
    
    //@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *bedNames = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", bedName];
    
    
    return [bedNames evaluateWithObject:candidate];
}


-(IBAction)SaveButton:(id)sender{
    
    if([self validate:[fieldNameCell text]] ==1)
	{ 
            
            if (keyIf == @"1") {
                
                NSArray *beds = [self.databaseUtility fetchedResultsWithClass:@"Bed" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",self.keyBedName]];
                
                Bed *bed = [beds lastObject];
                
                bed.name =fieldNameCell.text;
              //  bed.status =fieldStatusCell.text;

                
                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }else{
                
                Bed *bed = [NSEntityDescription insertNewObjectForEntityForName:@"Bed" inManagedObjectContext:self.databaseUtility.managedObjectContext];
                
                bed.name =fieldNameCell.text;
               // bed.status =fieldStatusCell.text;
                
                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        
        
	}else{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"NO" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		[alert show];
	}
    
}

//Animate
-(IBAction)btnb:(id)sender{
    NSLog(@"123");
    
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y - 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
    [UITableView commitAnimations];
    
}
/*
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textFieldDidBeginEditing");
    if (textField == fieldBirtdayCell) {
        [UITableView beginAnimations:nil context:NULL];
        [UITableView setAnimationDelegate:self];
        [UITableView setAnimationDuration:0.5];
        [UITableView setAnimationBeginsFromCurrentState:YES];
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y - 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
        [UITableView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"textFieldDidEndEditing");
    if (textField == fieldBirtdayCell) {
        [UITableView beginAnimations:nil context:NULL];
        [UITableView setAnimationDelegate:self];
        [UITableView setAnimationDuration:0.5];
        [UITableView setAnimationBeginsFromCurrentState:YES];
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y + 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
        [UITableView commitAnimations];
    }
    
}
*/
@end
